home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: typedefs for functions??
- Date: 8 Feb 1996 13:19:22 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4fct8q$2u1@sparcserver.lrz-muenchen.de>
- References: <4fa7u0$516@stork.runit.sintef.no>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- phi@bh.marintek.sintef.no (Per Henning Isaksen) writes:
-
-
-
- >The following code works as intended, but I would like to
- >change its 'looks'.
-
- > 1
- > 2 typedef void (*ActionFunction)(char* g);
- > 3
- > 4 void a(char* b, ActionFunction g) {
- > 5 ActionFunction p=g;
- > 6 (*p)(b);
- > 7 return;
- > 8 }
- > 9 void aa(char* g) { /* an ActionFunction */
- > 10 printf("..%s..\n", g);
- > 11 return;
- > 12 }
- > 13
- > 14
- > 15 void main() {
- > 16 a("adf", aa);
- > 17 }
-
- >Purpose: Inside a Motif GUI, I want to pass a function that
- >can perform some action, so I have the typdef in line 2 to
- >define ActionFunction.
-
- >I would like to change line 9 to:
- > 9 ActionFunction aa(char* g) {
- >But this causes a warning on line 16 (but it executes correctly)
- > warning 604: Pointers are not assignment-compatible.
- > warning 563: Argument #2 is not the correct type.
-
- "aa" is a function taking one argument that is a pointer to char
- and returning a pointer to a function that takes one argument
- that is a pointer to char and returns nothing. This is obviously
- not the type for which "ActionFunction" is an alias. Hence the
- warnings.
-
- >Of course, I could use a cast, but I wouldn't like it.
- >Anyone who can teach me the trick?
-
- The closest thing to what you want that comes to my mind is
- something like
-
- --------------8<----------8<--------------8<-----------------
- #include <stdio.h>
-
- typedef int f(int, int);
-
- typedef int (* pf)(int, int);
-
- f sum; /* This is a declaration for sum. Most compilers are
- able to warn you it the definition of a function
- does not match it's declaration. */
-
- int
- sum(int a, int b)
- {
- return a + b;
- }
-
- int
- main()
- {
- pf func;
-
- func = sum;
- printf("%d\n", (* func)(2, 3));
- return 0;
- }
- --------------8<----------8<--------------8<-----------------
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-
-